home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / VideoToolbox 95.04.18 / VideoToolboxSources / fp.c < prev    next >
Text File  |  1994-11-15  |  3KB  |  107 lines

  1. /* fp.c
  2. Apple provides a whole bunch of nice functions that are defined in fp.h, but
  3. unfortunately, at present they only exist on the PowerPC. As a stop-gap, until
  4. Apple provides them for 68k machines as well, I've written these 68k-compatible
  5. equivalents. You can safely include this file in all your projects. This file
  6. omits itself if Apple's versions of the functions are available.
  7.  
  8. At present this file only defines: ldtox80, x80told, and fpclassify. This replaces
  9. the functions formerly provided by the VideoToolbox Sane.c, which I've now discarded.
  10. You may also want to look at the functions defined in IsNan.c, which are very
  11. similar, though not identical to several fp.h functions.
  12.  
  13. ldtox80 and x80told convert back and forth between long double (any size)
  14. and 10-byte floating point formats. The 10- and 12-byte formats contain exactly
  15. the same information. The 12-byte format, used by the Motorola 68881 and 68040
  16. floating point chips, has an unused 2-byte gap between the exponent and mantissa.
  17.  
  18. HISTORY:
  19. 10/8/94 dgp wrote it.
  20. */
  21. #include "VideoToolbox.h"
  22. #include <math.h>        // ldtox80,x80told,fpclassify
  23. #if !defined(__FP__)    // Conditional applies to rest of file.
  24.  
  25. #if 0
  26.     #undef INF
  27.     #undef PI
  28.     #undef NAN
  29.     #include <SANE.h>    // x96tox80,x80tox96,extended80,extended96
  30. #else
  31.     // if extended80 and extended96 are undefined, change the "#if 0" to "#if 1" above.
  32.     void x96tox80(extended96 *,extended80 *);
  33.     void x80tox96(extended80 *,extended96 *);
  34. #endif
  35. #include <assert.h>
  36. void ldtox80 (long double *xl,extended80 *x80);
  37. void x80told (extended80 *x80,long double *xl);
  38. long int fpclassify(double x);
  39.  
  40. void ldtox80 (long double *xl,extended80 *x80)
  41. {
  42.     double x;
  43.     
  44.     switch(sizeof(long double)){
  45.     case 12:
  46.         x96tox80((extended96 *)xl,x80);
  47.         return;
  48.     case 10:
  49.         *x80=*(extended80 *)xl;
  50.         return;
  51.     default:
  52.         switch(sizeof(double)){
  53.         case 12:
  54.             x=*xl;
  55.             x96tox80((extended96 *)&x,x80);
  56.             break;
  57.         case 10:
  58.             *(double *)x80=*xl;
  59.             break;
  60.         default:
  61.             assert(0);
  62.         }
  63.     }
  64. }
  65.  
  66. void x80told (extended80 *x80,long double *xl)
  67. {
  68.     double x;
  69.  
  70.     switch(sizeof(long double)){
  71.     case 12:
  72.         x80tox96(x80,(extended96 *)xl);
  73.         break;
  74.     case 10:
  75.         *xl=*(long double *)x80;
  76.         break;
  77.     default:
  78.         switch(sizeof(double)){
  79.         case 12:
  80.             x80tox96(x80,(extended96 *)&x);
  81.             *xl=x;
  82.             break;
  83.         case 10:
  84.             *xl=*(double *)x80;
  85.             break;
  86.         default:
  87.             assert(0);
  88.         }
  89.     }
  90. }
  91.  
  92. pascal void MyClassExtended(extended80 *,short *) = { 0x3F3C,0x001C,0xA9EB };
  93.  
  94. long int fpclassify(double x)
  95. {
  96.     short n;
  97.     extended80 x80;
  98.     long double xl;
  99.  
  100.     xl=x;
  101.     ldtox80(&xl,&x80);
  102.     MyClassExtended(&x80,&n);
  103.     return n;
  104. }
  105.  
  106. #endif
  107.